package com.qfxl.common.util;

/**
 * @Title: EmailUtil
 * @Author cj
 * @Package com.qfxl.common.util
 * @Date 2025/9/24 17:07
 * @description:
 */

import org.springframework.util.ObjectUtils;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import java.util.Properties;

/** 邮箱工具类
 * @description: TODO
 * @author cj
 * @date 2025/9/24 17:07
 * @version 1.0
 */
public class EmailUtil {
    private Properties props ;
    /**
     * 邮箱会话
     */
    private Session session;
    /**
     * 发件人账号
     */
    private String username;
    /**
     * 激活码
     */
    private String password;
    /**
     * 是否开启调试
     */
    private boolean debug = true;
    /*
    封装复杂性、关注点分离
     */
    private EmailUtil(){}

    public class EmailConfig extends EmailUtil{

    }
    /**
     * 获取QQ邮箱配置
     */
    public static Properties getQQMailConfig(String username, String password) {
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.qq.com");
        props.setProperty("mail.port", "587");
        props.setProperty("mail.username", username);
        props.setProperty("mail.password", password);
        props.setProperty("mail.protocol", "smtp");
        props.setProperty("mail.auth", "true");
        props.setProperty("mail.debug", "false");
        props.setProperty("mail.smtp.starttls.enable", "true");
        return props;
    }

    /**
     * 获取163邮箱配置
     */
    public static Properties get163MailConfig(String username, String password) {
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.163.com");
        props.setProperty("mail.port", "25");
        props.setProperty("mail.username", username);
        props.setProperty("mail.password", password);
        props.setProperty("mail.protocol", "smtp");
        props.setProperty("mail.auth", "true");
        return props;
    }

    /**
     * 获取Gmail配置
     */
    public static Properties getGmailConfig(String username, String password) {
        Properties props = new Properties();
        props.setProperty("mail.host", "smtp.gmail.com");
        props.setProperty("mail.port", "587");
        props.setProperty("mail.username", username);
        props.setProperty("mail.password", password);
        props.setProperty("mail.protocol", "smtp");
        props.setProperty("mail.auth", "true");
        props.setProperty("mail.smtp.starttls.enable", "true");
        return props;
    }

    /**
     * 初始化邮件会话
     */
    private void initSession(Properties props) {
        // 设置认证信息
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        };

        this.session = Session.getInstance(props, authenticator);
        this.session.setDebug(debug);
    }

    public EmailUtil build(String username,String password,Properties properties){
        EmailUtil emailUtil;
        if (ObjectUtils.isEmpty(this)){
            emailUtil= new EmailUtil();
        }else {
            emailUtil = this;
        }
        emailUtil.initSession(properties);
        return emailUtil;
    }

    /*
    EmailUtil em = EmailUtil.build(username,password).properties();
    em.
     */

}
